<template>
  <view>
    <block v-for="(addressItem,index) in userAddressList" :key="index">
      <u-swipe-action>
        <u-swipe-action-item :options="Newoptions" @click="deleteAddressItem(addressItem)">
          <view class="address-item">
            <view class="left" @click="alreadySelected(addressItem)">
              <view class="name">
                {{addressItem.name}} {{addressItem.phone}}
              </view>
              <view class="address">
                {{addressItem.region}}{{addressItem.street}}
              </view>
            </view>
            <view class="right" @click="updateAddress(addressItem)">
              <uni-icons type="compose" size="30"></uni-icons>
            </view>
            <view class="mark" v-if="addressItem.defaults == 0">
              默认地址
            </view>
          </view>
        </u-swipe-action-item>
      </u-swipe-action>
    </block>
    <!-- 新建收货地址 -->

    <view @click="newAddAddress" class="newAddress">
      +新建收货地址
    </view>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        userAddressList: [],
        // defaultAddress: {},
        Newoptions: [{
          text: '删除',
          style: {
            backgroundColor: '#B8C1CC'
          }
        }],
      };
    },
    methods: {
      // 删除地址
      async deleteAddressItem(item) {
        const obj = {
          addressId: item.addressId
        }
        const res = await uni.$http.post('/personalcenter/addressrem', obj)
        if (res.data.code !== 200) return uni.$showMsg('删除失败')
        uni.$showMsg('删除成功')
        this.getUserAddress()

      },
      // 去新增地址页面
      newAddAddress() {
        uni.navigateTo({
          url: '/subpkg/newAddress/newAddress'
        })
      },
      // 选择收货地址
      alreadySelected(addressItemInfo) {
        // uni.redirectTo({
        //   url: `/subpkg/deliveryAddress/deliveryAddress?addressInfo=${encodeURIComponent(JSON.stringify(addressItemInfo))}`
        // })
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2]; // 上一个页面
        prevPage.$vm.otherFun(addressItemInfo);
        uni.navigateBack()
      },
      updateAddress(item) {
        uni.navigateTo({
          url: `/subpkg/updateAddress/updateAddress?addressInfo=${encodeURIComponent(JSON.stringify(item))}`
        })
      },
      // 获取用户地址信息
      async getUserAddress() {
        const res = await uni.$http.get('/personalcenter/address')
        this.userAddressList = res.data.data
        // 拿出默认展示地址信息
        // this.defaultAddress = res.data.data.find((item, index) => {
        //   return item.defaults == '0'
        // })
      }
    },
    onShow() {
      this.getUserAddress()
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #F5F6F9;
  }



  .address-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #fff;
    padding: 10px;
    position: relative;

    .left {
      width: 240px;

      .name {
        font-size: 18px;
        color: #000000;
        margin-bottom: 20rpx;
      }

      .address {
        font-size: 14px;
        color: #8F9CA9;
        word-wrap: break-word;
      }
    }

    .mark {
      width: 80px;
      height: 20px;
      background-color: #0062D9;
      color: white;
      line-height: 20px;
      text-align: center;
      border-radius: 12px;
      position: absolute;
      top: 12px;
      left: 210px;
    }
  }

  .newAddress {
    width: 400rpx;
    margin: 20rpx auto;
    height: 70rpx;
    background-color: #000000;
    color: white;
    line-height: 70rpx;
    border-radius: 14px;
    text-align: center;
  }
</style>